﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OCR.Config
{
    internal class LocalTemplateInfo
    {
        public static string Info01;
        public static string Info02;
        public static string Info03;
        public static string Info04;
        public static string Info05;
        public static string Info06;

        public static string rect_X;
        public static string rect_Y;
        public static string rect_Right;
        public static string rect_Bottom;

        public static string path;

        public static List<string> InfoList=new List<string>() { Info01 , Info02, Info03, Info04, Info05, Info06};

        public static Rectangle rectangle;
        public static void LocalTemplateInfoInitial()
        {
            LocalTemplate_Config localTemplate_Config = new LocalTemplate_Config();
            StreamReader sr = new StreamReader("Config\\LocalTemplateInfo.json");
            string re = sr.ReadToEnd();
            localTemplate_Config = JsonMapper.ToObject<LocalTemplate_Config>(re);
            Info01 = localTemplate_Config.Info01;
            Info02 = localTemplate_Config.Info02;
            Info03 = localTemplate_Config.Info03;
            Info04 = localTemplate_Config.Info04;
            Info05 = localTemplate_Config.Info05;
            Info06 = localTemplate_Config.Info06;

            rect_X = localTemplate_Config.rect_X;
            rect_Y = localTemplate_Config.rect_Y;
            rect_Right = localTemplate_Config.rect_Right;
            rect_Bottom = localTemplate_Config.rect_Bottom;

            path = localTemplate_Config.path;

            //Console.WriteLine(Info04);
            InfoList = new List<string>() { Info01, Info02, Info03, Info04, Info05, Info06 };
            for (int i = 0; i < InfoList.Count; i++)
            {
                Console.WriteLine("初始化本地模板信息" + i + ":  " + InfoList[i]);
            }
            Console.WriteLine("初始化本地模板图片: "+path);

            sr.Dispose();
            sr.Close();

            if (path == null)
            {
                MessageBox.Show("模板图片信息为null");
            }

            if (path!= null&&!File.Exists(path))
            {
                MessageBox.Show("不存在本地模板图片");
            }

            rectangle = new Rectangle
(int.Parse(rect_X) * 2,
int.Parse(rect_Y) * 2,
(int.Parse(rect_Right) - int.Parse(rect_X)) * 2,
(int.Parse(rect_Bottom) - int.Parse(rect_Y)) * 2);
        }

        public static void LocalTemplateInfoSave(List<string> infos) 
        {
            LocalTemplate_Config localTemplate_Config = new LocalTemplate_Config();
            localTemplate_Config.Info01
                = localTemplate_Config.Info02
                = localTemplate_Config.Info03
                = localTemplate_Config.Info04
                = localTemplate_Config.Info05
                = localTemplate_Config.Info06=string.Empty;

            if (infos.Count >= 1)
                localTemplate_Config.Info01 = Info01 = infos[0]; 
            if (infos.Count >= 2)
                localTemplate_Config.Info02 = Info02 = infos[1];
            if (infos.Count >= 3)
                localTemplate_Config.Info03 = Info03 = infos[2];
            if (infos.Count >= 4)
                localTemplate_Config.Info04 = Info04 = infos[3];
            if (infos.Count >= 5)
                localTemplate_Config.Info05 = Info05 = infos[4];
            if (infos.Count >= 6)
                localTemplate_Config.Info06 = Info06 = infos[5];

            localTemplate_Config.rect_X
              = localTemplate_Config.rect_Y
              = localTemplate_Config.rect_Right
              = localTemplate_Config.rect_Bottom= string.Empty;

            if (rect_X!=null)
                localTemplate_Config.rect_X = rect_X;
            if (rect_Y!=null)
                localTemplate_Config.rect_Y = rect_Y;
            if(rect_Right!=null)
                localTemplate_Config.rect_Right = rect_Right;
            if (rect_Bottom!=null)
                localTemplate_Config.rect_Bottom = rect_Bottom;

            localTemplate_Config.path = path;

            

            //Console.WriteLine(localTemplate_Config.Info04);
            string json = JsonMapper.ToJson(localTemplate_Config);

            Console.WriteLine(JsonUTF8toUnicode(json));
            File.WriteAllText("Config\\LocalTemplateInfo.json", JsonUTF8toUnicode(json));
        }

        /// <summary>
        /// UTF-8格式json打印乱码 问题解决方式
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string JsonUTF8toUnicode(string jsonStr)
        {
            Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
            var str = reg.Replace(jsonStr, delegate (Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });
            return str;
        }


        public static bool CompareTemplate(List<TemplateConfig> template_Config)
        {
            LocalTemplateInfoInitial();
            InfoList = new List<string>() { Info01, Info02, Info03, Info04, Info05, Info06 };
            bool isSame = false;
            if (template_Config.Count <6)
            {
                return false;
            }
            else
            {
                for (int i = 0; i < InfoList.Count; i++)
                {
                    Console.WriteLine($"算法识别：{template_Config[i].text}   "+ $"本地模板：{InfoList[i]}" );
                    if (template_Config[i].text != InfoList[i])
                    {
                        if (InfoList[i] == string.Empty)
                        {
                            isSame = true;
                        }
                        else
                        {
                            isSame = false;
                            break;
                        }

                    }
                    else
                    {
                        isSame= true;
                    }
                }
                if (isSame)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }    

        }

        public static bool CompareTemplate(List<string> template_Config)
        {
            //LocalTemplateInfoInitial();
            InfoList = new List<string>() { Info01, Info02, Info03, Info04, Info05, Info06 };
            bool isSame = false;
            if (template_Config.Count < 6)
            {
                return false;
            }
            else
            {
                for (int i = 0; i < InfoList.Count; i++)
                {
                    Console.WriteLine($"算法识别：{template_Config[i]}   " + $"本地模板：{InfoList[i]}");
                    if (template_Config[i] != InfoList[i])
                    {
                        if (InfoList[i] == string.Empty)
                        {
                            isSame = true;
                        }
                        else
                        {
                            isSame = false;
                            break;
                        }

                    }
                    else
                    {
                        isSame = true;
                    }
                }
                if (isSame)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }

        }
        //无序对比
        public static bool CompareTemplate_Disorder(List<string> template_Config)
        {
            //LocalTemplateInfoInitial();
            InfoList = new List<string>() { Info01, Info02, Info03, Info04, Info05, Info06 };

            if (template_Config.Count < 6)
            {
                return false;
            }
            else
            {
                if (template_Config == null || InfoList == null)
                    return template_Config == InfoList;

                if (template_Config.Count != InfoList.Count)
                    return false;
                List<string> uniqueInArr1 = template_Config.Except(InfoList).ToList();

                LocalInfo.wrongIndex.Clear();

                for (int i = 0; i < template_Config.Count(); i++)
                {
                    for (int j = 0; j < uniqueInArr1.Count(); j++)
                    {
                        if (template_Config[i] == uniqueInArr1[j])
                        {
                            LocalInfo.wrongIndex.Add(i);
                            Console.WriteLine("uniqueInArr1:" + uniqueInArr1[j] + " index: " + i);
                        }
                    }
    
                }

                return template_Config.OrderBy(x => x).SequenceEqual(InfoList.OrderBy(x => x));
            }

        }

        public static bool CompareTemplate_Disorder(List<string> template_Config, List<string> imgInfos)
        {
            if (template_Config == null || imgInfos == null)
                return template_Config == imgInfos;

            if (template_Config.Count != imgInfos.Count)
                return false;

            //List<string> uniqueInArr1 = template_Config.Except(imgInfos).ToList();

            //LocalInfo.wrongIndex.Clear();

            //for (int i = 0; i < template_Config.Count(); i++)
            //{
            //    for (int j = 0; j < uniqueInArr1.Count(); j++)
            //    {
            //        if (template_Config[i] == uniqueInArr1[j])
            //        {
            //            LocalInfo.wrongIndex.Add(i);
            //            Console.WriteLine("uniqueInArr1:" + uniqueInArr1[j] + " index: " + i);
            //        }
            //    }

            //}

            return template_Config.OrderBy(x => x).SequenceEqual(imgInfos.OrderBy(x => x));

        }
    }
}
